package dao;

import model.Game;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 * Created by VLAD on 08.07.2014.
 */
public class GameDao implements IGameDao
{
    @Override
    public long saveGame(Game game) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        Long gameID = null;
        tx = session.beginTransaction();
        gameID = (Long) session.save(game);
        tx.commit();
        return gameID;
    }

    @Override
    public Game getGameByID(long id) {
        Game game = null;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        Long gameID = null;
        tx = session.beginTransaction();
        game = (Game)session.get(Game.class, id);
        tx.commit();
        return game;
    }

    @Override
    public void updateGame(Game game) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        Long gameID = null;
        tx = session.beginTransaction();
        session.update(game);
        tx.commit();
    }
}
